package com.hxd.mall.module.usercenter.invitataion

import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import androidx.core.view.get
import com.alibaba.android.arouter.facade.annotation.Route
import com.hexin.locallibrary.permssion.PermissionGrantedCallback
import com.hexin.locallibrary.permssion.PermissionList
import com.hexin.locallibrary.permssion.PermissionUtils
import com.hxd.mall.R
import com.hxd.mall.base.BaseInjectActivity
import com.hxd.mall.constant.HxdConstantsRouterPath.PathUserCenter.path_user_center_nick
import com.hxd.mall.utils.local.*
import com.hxd.mall.utils.open.*
import com.hxd.mall.widget.coverflow.CoverFlow
import com.hxd.threelibrary.umeng.UmClient
import crossoverone.statuslib.StatusUtil
import kotlinx.android.synthetic.main.activity_invivation_layout.*
import kotlinx.android.synthetic.main.home_top_fg_layout.view.*
import org.jetbrains.anko.dip
import org.jetbrains.anko.displayMetrics

/**
 * 昵称修改页
 */
@Route(path = path_user_center_nick)
class InvivationActivity : BaseInjectActivity<IInvitationPresenter>(), IInvivationView {

    override fun attachLayoutRes(): Int = R.layout.activity_invivation_layout

    override fun initViews() {
        setHeightAndWidth()
        setTitleCommon(getString(R.string.invite_title))
        addSetOnClickListener(codeCopy, tv_share) {
            onClick(it)
        }
    }

    fun setHeightAndWidth() {
        val params = view_pager.layoutParams
        params.width = displayMetrics.widthPixels * 3 / 5
        params.height = params.width * 27 / 14
        view_pager.layoutParams = params
    }

    override fun initData() {
        mPresenter?.getData()
    }

    override fun showData(posterBean: PosterBean?) {
        if (posterBean?.data == null)
            return
        tv_code.text = posterBean.data.invitationCode ?: ""
        CoverFlow.Builder()
            .with(view_pager)
            .pagerMargin(0f)
            .scale(0.1f)
            .spaceSize(70f)
            .rotationY(0f)
            .build()

        view_pager.adapter = CardAdapter(this, posterBean.data)
        view_pager.offscreenPageLimit = posterBean.data.bgImgList.size
//        view_pager.clipChildren = false
//        view_pager.setPageTransformer(true, CardTransformer())
    }

    override fun setStatusColor() {
        StatusUtil.setUseStatusBarColor(this, Color.WHITE)
    }

    override fun setSystemInvadeBlack() {
        StatusUtil.setSystemStatus(this, false, true)
    }

    private fun onClick(it: View) {
        when (it) {
            codeCopy -> {
                sendPoint(cope_button_click)
                setPrimaryClip(this, tv_code.text.toString())
            }
            tv_share -> {
                sendPoint(poster_share_clicke)
                if (view_pager.childCount == 0) {
                    ToastUtil.showToast("暂无邀请图片")
                    return
                }
                showShareFriendDialog { type ->
                    //val bitmap = convertViewToBitmap((view_pager[view_pager.currentItem] as ViewGroup).getChildAt(0))
                    val bitmap = convertViewToBitmap(view_pager[view_pager.currentItem])

                    if (bitmap == null) {
                        ToastUtil.showToast("图片获取失败")
                        return@showShareFriendDialog
                    }
                    when (type) {
                        UmClient.WEIXIN -> {
                            sendPoint(wechat_path_button_click)
                            UmClient.shareScreenShot(this, UmClient.WEIXIN, bitmap)
                        }
                        UmClient.WEIXIN_CIRCLE -> {
                            sendPoint(moments_path_button_click)
                            UmClient.shareScreenShot(this, UmClient.WEIXIN_CIRCLE, bitmap)
                        }
                        UmClient.SAVE_PIC -> {
                            sendPoint(download_button_click)
                            PermissionUtils.permissionCheck(this, PermissionList.permission_sdcardRW, "", object : PermissionGrantedCallback {
                                override fun granted() {
                                    saveBitmap(bitmap, this@InvivationActivity)
                                }

                                override fun denied() {
                                }
                            })
                        }
                    }
                }
            }
        }
    }

}
